package com.bug;


import com.alibaba.fastjson.JSON;
import com.model.FamilyWord;
import com.model.ExplainDetail;
import org.apache.logging.log4j.util.Strings;
import org.jsoup.Jsoup;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class VocabularyCatchStrategy extends CatchStrategy{

    @Override
    public List<CatchPage> getInfoCatchList(String word) {
        //
        CatchPage page1 = new CatchPage();
        List<Method> methods1 = new ArrayList<>();
        methods1.add(EXPLAIN);
        methods1.add(UNDERSTAND);
        methods1.add(DEFINITIONS_JSON);
        methods1.add(APPEAR);
        page1.setUrl("https://www.vocabulary.com/dictionary/definition.ajax?search="+word+"&lang=en");
        page1.setChain(methods1);
        page1.setWord(word);

        List<CatchPage> list = new ArrayList<>();
        list.add(page1);
        return list;
    }

    @Override
    public List<CatchPage> getExampleCatch(String word) {
        return null;
    }

    @Override
    public String onCatchPage(Explain method, Html html, String word) {
        String explain = "";
        try{
            explain = Jsoup.parse(html.$(".main").$(".blurb").$(".short").get()).text();
        }catch (Exception e) {};
        return explain;
    }

    @Override
    public String onCatchPage(Understand method, Html html, String word) {
        String understand = "";
        try{
            understand = Jsoup.parse(html.$(".main").$(".blurb").$(".long").get()).text();
        }catch (Exception e) {};
        return understand;
    }

    @Override
    public List<ExplainDetail> onCatchPage(Definitions_json method, Html html, String word) {
        // 获取每一组解释
        List<Selectable> nodes = html.$(".group").$(".ordinal").$(".sense").nodes();

        List<ExplainDetail> details = new ArrayList<>(nodes.size());

        nodes.forEach(node->{
            // 解析类型
            String tip = Jsoup.parse(node.$(".anchor").get()).text();
            ExplainDetail detail = new ExplainDetail();
            details.add(detail);
            detail.setTypeByStr(tip);
            // 解释文本获取
            String explain = Jsoup.parse(node.$(".definition").nodes().get(0).get()).text();
            explain = explain.substring(explain.indexOf(" ") + 1);
            detail.setExplained(explain);
            // 同义词, 类型词
            List<Selectable> instances = node.$(".instances").nodes();
             if(!instances.isEmpty() && instances.size() > 0) {
                instances.forEach(instance -> {
                    String type = Jsoup.parse(instance.$("dt").get()).text();
                    List<Selectable> vocabularys = instance.$(".word").nodes();
                    List vocabularyList = new ArrayList<>(vocabularys.size());
                    vocabularys.forEach(vocabulary -> {
                        vocabularyList.add(Jsoup.parse(vocabulary.get()).text());
                    });

                    switch (type) {
                        case "Synonyms:":
                            if(detail.getSynonyms() == null) {
                                detail.setSynonyms(vocabularyList);
                            }else {
                                detail.getSynonyms().addAll(vocabularyList);
                            }

                            break;
                        case "Type of:":
                        case "Types:":
                            if(detail.getTypes() == null) {
                                detail.setTypes(vocabularyList);
                            }else {
                                detail.getTypes().addAll(vocabularyList);
                            }
                            break;
                    }
                });
            }

            List<Selectable> ex_nodes = node.$(".example").nodes();
            if (!ex_nodes.isEmpty() && ex_nodes.size() > 0 ) {
                // 例句集合文本获取
                List<String> examples = new ArrayList<>(ex_nodes.size());
                ex_nodes.forEach(ex_node -> {
                    String ex_part = Jsoup.parse(ex_node.get()).text();
                    examples.add(ex_part);
                });
                detail.setExamplesJson(JSON.toJSONString(examples));
            }
        });

        return details;
    }

    @Override
    public String onCatchPage(Appear method, Html html, String word) {
        String dataJson = Jsoup.parse(html.$(".family").$("[data]").get()).getElementsByAttribute("data").attr("data");
        if(Strings.isEmpty(dataJson)) return null;

        List<FamilyWord> appears = JSON.parseArray(dataJson, FamilyWord.class);
        // family(同源词)获取
        appears.remove(0);
        List<String> familyWords = appears.stream().map((appear) -> appear.getWord()).collect(Collectors.toList());
        familyWords.remove(word);
        this.wordInfoManage.getWordInfo(word).setFamilyWord(familyWords);
        float appear = 1 + 1 / (appears.get(0).getFfreq() / 4000);
        return "1/"+appear;
    }

    @Override
    public void onCatchPage(Synonyms method, Html html, String word) {

    }

    @Override
    public void onCatchPage(Type method, Html html, String word) {

    }

    @Override
    public void onCatchPage(Example method, Html html, String word) {

    }




}
